<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size">
        <el-form-item>
          <kt-button icon="fa fa-plus" :label="$t('action.add')" perms="organization:add" type="primary" @click="handleAdd"/>
        </el-form-item>
      </el-form>
    </div>
    <div class="Document">
      <el-table :data="tableData" stripe size="mini" style="width: 100%;"
                rowKey="id" v-loading="loading"  cell-style="height:50px" fit="false">
        <el-table-column prop="id" header-align="center" align="center" width="100"  label="ID">
        </el-table-column>
        <el-table-column prop="name" header-align="center" align="center" width="200" label="名称">
        </el-table-column>
        <el-table-column prop="description" header-align="center" align="center"  label="内容" >
        </el-table-column>
        <el-table-column
          fixed="right" header-align="center" align="center" width="185" :label="$t('action.operation')">
          <template slot-scope="scope">
            <kt-button icon="fa fa-edit" :label="$t('action.edit')" perms="organization:edit" @click="handleEdit(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增修改界面 -->
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" width="70%" :visible.sync="dialogVisible" :close-on-click-modal="false">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="submitForm()"
               label-width="80px" :size="size" style="text-align:left;">
        <el-form-item label="名称" prop="name">
          <el-input v-model="dataForm.name" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item  label="排序编号" prop="orderNum">
          <el-input-number v-model="dataForm.seq" controls-position="right" :min="0" label="排序编号"></el-input-number>
        </el-form-item>
        <el-form-item label="内容"  prop="description">
          <quill-editor
            v-model="dataForm.description"
            ref="myQuillEditor"
            :options="quillOption"
          ></quill-editor>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :size="size"  @click="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button :size="size"  type="primary" @click="submitForm()">{{$t('action.comfirm')}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import KtButton from "@/views/Core/KtButton"
  import quillConfig from '@/utils/quill-config'
  export default {
    components:{
      KtButton
    },
    data() {
      return {
        size: 'small',
        loading: false,
        filters: {
          name: ''
        },
        tableData: [],
        menuData: [],
        dialogVisible: false,
        quillOption: quillConfig,
        dataForm: {
          id: 0,
          name: '',
          seq: 0,
          description:""
        },
        dataRule: {
          name: [
            { required: true, message: '文档名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 获取数据
      findTreeData: function () {
        this.loading = true
        this.$api.doc.findPage({}).then((res) => {
          console.log(res.data)
          this.tableData = res.data;
          this.loading = false
        })
      },
      // 显示新增界面
      handleAdd: function () {
        this.dialogVisible = true
        this.dataForm = {
          id: 0,
          name: '',
          seq: 0,
          description:'',
        }
      },
      // 显示编辑界面
      handleEdit: function (row) {
        this.dialogVisible = true
        Object.assign(this.dataForm, row);
      },
      // 删除
      /*handleDelete (row) {
        this.$confirm('确认删除选中记录吗？', '提示', {
                  type: 'warning'
        }).then(() => {
          let params = this.getDeleteIds([], row)
          this.$api.dept.batchDelete(params).then( res => {
            this.findTreeData()
            this.$message({message: '删除成功', type: 'success'})
          })
        })
      },*/
      // 表单提交
      submitForm () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              let params = Object.assign({}, this.dataForm)
              if(params.id == 0) {
                this.$api.doc.add(params).then((res) => {
                  if (res.code == 200) {
                    this.$message({message: '操作成功', type: 'success'})
                    this.dialogVisible = false
                    this.$refs['dataForm'].resetFields()
                  } else {
                    this.$message({message: '操作失败, ' + res.msg, type: 'error'})
                  }
                  this.findTreeData()
                })
              }else {
                this.$api.doc.edit(params).then((res) => {
                  if (res.code == 200) {
                    this.$message({message: '操作成功', type: 'success'})
                    this.dialogVisible = false
                    this.$refs['dataForm'].resetFields()
                  } else {
                    this.$message({message: '操作失败, ' + res.msg, type: 'error'})
                  }
                  this.findTreeData()
                })
              }
            })
          }
        })
      },
    },
    mounted() {
      this.findTreeData()
    }
  }
</script>

<style scoped>

</style>
